<template>
  <div>
    <el-form ref="workLogForm" size="small" :model="entity" :rules="rules" label-width="120px">
      <el-form-item label="日期" prop="logDate">
        <el-date-picker v-model="entity.logDate" type="date" value-format="yyyy-MM-dd" placeholder="日期" style="width: 100%;" />
      </el-form-item>
      <el-form-item label="用时/小时" prop="useHour">
        <el-input v-model="entity.useHour" type="number" oninput="if(isNaN(value)) { value = null } if(value.indexOf('.')>0){value=value.slice(0,value.indexOf('.')+3)}" />
      </el-form-item>
      <el-form-item label="内容" prop="logText">
        <el-input type="textarea" resize="none" :autosize="{ minRows: 5, maxRows: 5 }" v-model.number="entity.logText" placeholder="内容" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from '@/utils/request'
import dayjs from 'dayjs'

export default {
  props:{
    entityId : {type : Number, default: 0}
  },
  data() {
    return {
      entity: {
        logText: '',
        useHour: '',
        logDate: '',
      },
      rules: {
        logText: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          //{ min: 1, max: 1000, message: '长度在 1 到 1000 个字符', trigger: 'blur' }
        ],
        useHour: [
          { required: true, message: '请输入用时', trigger: 'blur' }
        ],
        logDate: [
          { required: true, message: '请输入日志日期', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.loadEntity();
  },
  methods: {
    loadEntity(){
      if(this.entityId > 0){
        request({ url: '/worklog/getworklog', data: {id : this.entityId} }).then( res =>{
          this.entity = res.data
          this.entity.logDate=dayjs(this.entity.logDate).format("YYYY-MM-DD")
        })
      }else{
        this.entity.logDate= dayjs().format("YYYY-MM-DD")
      }
    },
    onSubmit(){
      this.$refs['workLogForm'].validate((valid) => {
        if(valid){
          request({
            url: '/worklog/addeditworklog',
            data: this.entity
          }).then( res =>{
            if(res.code === "200"){
              this.$message.success(res.msg)
              this.$emit('update:visible', false);  //关闭弹窗
              //触发事件总线，让列表界面更新数据
              this.entityId > 0 ? this.$EventBus.$emit("worklogedit", {}):this.$EventBus.$emit("worklogadd", {})
            }else{
              this.$message.error(res.msg);
            }
          })
        }else{
          console.log('error submit!!');
          return false;
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
